﻿using System;
using System.Text;
using QuanLyHopDong.Biz;

namespace QuanLyHopDong.Web.HopDong
{
    public partial class TienDoThiCongExport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var donVi = Request.Params["donVi"];
            var soHopDong = Request.Params["soHopDong"];
            int tinhTrangThiCong, tinhTrangVatTu;
            if (!int.TryParse(Request.Params["tinhTrangThiCong"], out tinhTrangThiCong))
            {
                return;
            }
            if (!int.TryParse(Request.Params["tinhTrangVatTu"], out tinhTrangVatTu))
            {
                return;
            }
            if (tinhTrangThiCong < 0 || tinhTrangVatTu < 0)
            {
                return;
            }
            var matram = Request.Params["matram"];
            int trangThaiHopDongId, doiThiCongId;
            if (!int.TryParse(Request.Params["trangThaiHopDongId"], out trangThaiHopDongId))
            {
                return;
            }
            if (!int.TryParse(Request.Params["doiThiCongId"], out doiThiCongId))
            {
                return; 
            }

            var html = GenerateHtml(donVi, soHopDong, tinhTrangThiCong, tinhTrangVatTu, matram, doiThiCongId, trangThaiHopDongId);
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition",
                string.Format("attachment;filename=Tien_Do_Thi_Cong_{0:dd-MM-yyyy_hhmmss}.xls", DateTime.Now));
            Response.ContentEncoding = Encoding.UTF8;
            Response.ContentType = "application/vnd.ms-excel";
            Response.BinaryWrite(Encoding.Unicode.GetPreamble());
            var bytes = Encoding.Unicode.GetBytes(html);
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }

        private static string GenerateHtml(string tenDonVi, string soHopDong, int tinhTrangThiCongId, int tinhTrangVatTuId, string matram, int doiThiCongId, int trangThaiHopDongId)
        {
            var khoiLuongs = new KhoiLuongBiz().ExportKhoiLuongs(tenDonVi, soHopDong, tinhTrangThiCongId, tinhTrangVatTuId, matram, doiThiCongId, trangThaiHopDongId);

            var headers = new[]
            {
                "Số HĐ", "Tên HĐ", "Đơn vị", "Loại dự án", "Mã tuyến", "Tiến độ thi công", "Ghi chú", "Nhận vật tư A", "Điểm đầu", "Điểm cuối", "Chiều dài tuyến", "Loại cáp"
            };

            var sbHtml = new StringBuilder();
            sbHtml.Append("<html><body style='font-size:12px !important;font-family:Times New Roman !important;'><head><meta charset=\"utf-8\" />");
            sbHtml.Append("<style type='text/css'>td {vertical-align:top;}</style>");
            sbHtml.Append("</head><body>");
            sbHtml.Append("<table cellpadding='0' cellspacing='0' width='100%' style='border-collapse:collapse;'>");

            // headers
            sbHtml.Append("<tr>");
            foreach (var header in headers)
            {
                sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:center;'><strong>{0}</strong></td>", header);
            }
            sbHtml.Append("</tr>");

            foreach (var khoiLuongHopDongView in khoiLuongs)
            {
                sbHtml.Append("<tr>");
                sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:left;'>{0}</td>", khoiLuongHopDongView.SoHopDong);
                sbHtml.AppendFormat("<td style='border:solid 0.1pt;'>{0}</td>", khoiLuongHopDongView.TenHopDong);
                sbHtml.AppendFormat("<td style='border:solid 0.1pt;'>{0}</td>", khoiLuongHopDongView.TenDonVi);
                sbHtml.AppendFormat("<td style='border:solid 0.1pt;'>{0}</td>", khoiLuongHopDongView.LoaiDuAn);
                sbHtml.AppendFormat("<td style='border:solid 0.1pt;'>{0}</td>", khoiLuongHopDongView.MaTuyen);
                sbHtml.AppendFormat("<td style='border:solid 0.1pt;'>{0}</td>", khoiLuongHopDongView.TinhTrangThiCong);
                sbHtml.AppendFormat("<td style='border:solid 0.1pt;'>{0}</td>", khoiLuongHopDongView.GhiChu);
                sbHtml.AppendFormat("<td style='border:solid 0.1pt;'>{0}</td>", khoiLuongHopDongView.TinhTrangVatTu);
                sbHtml.AppendFormat("<td style='border:solid 0.1pt;'>{0}</td>", khoiLuongHopDongView.DiemDau);
                sbHtml.AppendFormat("<td style='border:solid 0.1pt;'>{0}</td>", khoiLuongHopDongView.DiemCuoi);
                sbHtml.AppendFormat("<td style='border:solid 0.1pt;'>{0}</td>", khoiLuongHopDongView.ChieuDaiTuyen);
                sbHtml.AppendFormat("<td style='border:solid 0.1pt;'>{0}</td>", khoiLuongHopDongView.TenLoaiCap);
                sbHtml.Append("</tr>");
            }

            sbHtml.Append("</table>");
            sbHtml.Append("</body></html>");

            return sbHtml.ToString();
        }
    }
}